package gui;

import processing.core.PApplet;

public class Toolbar extends Widget {

  private Widget[] elements;
  private float spacing;
  private int length = 0; // number of defined elements
  private float unoccupiedX0, unoccupiedWidth;
  private boolean hidden = false;
  private Slider lastSliderAdded; // this is a kluge to make the "below" option work: elements[length-1].x0 returns 0 when elements[length-1] is a slider
  private Widget lastUpdated = null;
  private int lastUpdatedIndex = -1;
  
  public Toolbar(PApplet parent_applet, float[] r, int maxElements, Object... args) {
	super(parent_applet, r, args);
    elements = new Widget[maxElements];
    spacing = height/2;
    unoccupiedX0 = x0;
    unoccupiedWidth = width;
  }
  
  private Widget lastAdded() {
    return elements[length-1];
  }
  
  /*
  Widget find(String nm) {
    Widget theOne = null;
    boolean found = false;
    for (int i=0; ((i<length) && (!found)); i++) {
      found = name.equals(elements[i].name);
      if (found) theOne = elements[i];
    }
    return theOne;
  }
  */
  
  private Widget addElement(Widget E) {
    if (length < elements.length) {
      length++;
      elements[length-1] = E;
      return E;
    } else {
      return null;
    }    
  }

  /*
  // to add an element to the toolbar, use one of the following: arguments match the constructors for
  // each class, but replace the position rectangle with "left" or "right."
  TextButton addTextButton(String name0, String alignmt, int neutral, int actv) {
    float wd = PApplet.max(height, textWidth("   "+name0+"   "));
    return (TextButton) addElement(new TextButton(name0, nextPosition(alignmt, wd), neutral, actv));
  }
  */
  
  PolyButton addPolyButton(String name0, String alignmt, int neutral, int actv, float[] xx, float[] yy) {
    return (PolyButton) addElement(new PolyButton(pa, nextPosition(alignmt,height), name0, neutral, actv, xx, yy));
  }
  
/*
  MultistatePolyButton addMultistatePolyButton(String name0, int N, String alignmt) {
    return (MultistatePolyButton) addElement(new MultistatePolyButton(name0, N, nextPosition(alignmt, height)));
  }

  Slider addSlider(String name0, String alignmt, float minVal, float maxVal) {
    float sliderWidth = 10*height/3;
    return addSlider(name0, alignmt, minVal, maxVal, sliderWidth);
  }
  
  Slider addSlider(String name0, String alignmt, float minVal, float maxVal, float wd) {
    float ht = (float) 0.4*height;
    float[] pos = nextPosition(alignmt, wd);
    pos[1] += (pos[3]-ht)/2;
    pos[3] = ht;
    Slider S = new Slider(name0, pos, neutral, actv, minVal, maxVal);
    addElement(S);
    lastSliderAdded = S;
    setFont(S.nameTextSize);
    float nameWidth = textWidth(S.name + " ");
    float valWidth = PApplet.max(textWidth(S.val2string(S.pos2val(0))+" "), textWidth(S.val2string(S.pos2val(1))+" "));
    if (alignmt.equals("left") || alignmt.equals("LEFT")) {
      S.x0 += nameWidth;
      unoccupiedX0 += (nameWidth + valWidth);
      unoccupiedWidth -= (nameWidth + valWidth);
    } 
    else {
      if (alignmt.equals("right") || alignmt.equals("RIGHT")) {
        S.x0 -= valWidth;
        unoccupiedWidth -= (nameWidth + valWidth);
      }
    } 
    return S;
  }
  */
  
  private float[] unoccupied() {
    float[] r = {unoccupiedX0, y0, unoccupiedWidth, height};
    return r;
  }

  private float[] nextPosition(String alignmt, float dx) {  
    float dxtot = dx;
    float[] r;
    if (alignmt.equals("left") || alignmt.equals("LEFT")) { // in the main toolbar, aligned left
      if (unoccupiedX0 > x0) {dxtot += spacing;} // add spacer unless all the way at the left
      unoccupiedX0 += dxtot;
      unoccupiedWidth -= dxtot;
      r = new float[] {unoccupiedX0 - dx, y0, dx, height};
    } else if (alignmt.equals("right") || alignmt.equals("RIGHT")) { // in the main toolbar, aligned right
      if (unoccupiedX0+unoccupiedWidth < x0+width) {dxtot += spacing;} // add spacer unless all the way at the right
      unoccupiedWidth -= dxtot;
      r = new float[] {unoccupiedX0 + unoccupiedWidth, y0, dx, height};
    } else if (alignmt.equals("below") || alignmt.equals("BELOW")) { // directly below the last element defined
      // note: when the last element is a slider, the line below returns 0
      float xx = elements[length-1].x0;
      float yy = elements[length-1].y0 + elements[length-1].height + spacing;
      if (elements[length-1] instanceof Slider) {
        xx = lastSliderAdded.x0;
        yy = (float) (lastSliderAdded.y0 + 1.5*lastSliderAdded.height);
      }
      r = new float[] {xx, yy, dx, height};
    } else {
      r = new float[] {0,0,0,0};
    }
    return r;
  }    
   
}
